package com.ssy.lingxi.component.rabbitMQ.serviceImpl;

import com.ssy.lingxi.component.rabbitMQ.service.IRabbitMQUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * RocketMQ实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-06-28
 */
@Service("IRocketMqUtils")
@ConditionalOnExpression("T(org.springframework.util.StringUtils).hasLength('${rocketmq.name-server:}')")
public class RocketMqUtilsImpl implements IRabbitMQUtils {
    @Resource
    private RocketMQTemplate rocketMqTemplate;

    /**
     * 发送信息
     *
     * @param exchange RocketMq的队列名称
     * @param obj      消息体
     */
    @Override
    public void sendMsg(String exchange, Object obj) {
        rocketMqTemplate.syncSend(exchange, MessageBuilder.withPayload(obj).build());
    }

    /**
     * 发送信息
     *
     * @param exchange   队列名称
     * @param routingKey 路由键
     * @param obj        消息体
     */
    @Override
    public void sendMsg(String exchange, String routingKey, Object obj) {
        rocketMqTemplate.syncSend(exchange, MessageBuilder.withPayload(obj).build());
    }

    /**
     * 发送信息并且等消费端返回
     *
     * @param exchange   队列名称
     * @param routingKey 路由键
     * @param obj        消息体
     * @param timeOut    超时时间
     */
    @Override
    public Object sendAndReceiveMsg(String exchange, String routingKey, Object obj, long timeOut) {
        return rocketMqTemplate.sendAndReceive(exchange, MessageBuilder.withPayload(obj).build(), obj.getClass(), timeOut);
    }

    /**
     * 发送信息
     *
     * @param exchange   队列名称
     * @param routingKey 路由键
     * @param obj        消息体
     * @param delayTime  延迟时间(单位为ms)
     */
    @Override
    @Deprecated
    public void sendMsg(String exchange, String routingKey, Object obj, long delayTime) {

    }

    /**
     * 接收消息
     *
     * @param queueName 队列名称
     */
    @Override
    public Object receiveMsg(String queueName) {
        return null;
    }
}
